<?php

namespace Home\Model;

use Think\Model;

class ApiOrderModel extends Model
{
    public function __construct()
    {
        parent::__construct('order');
    }


    /**
     * 读取订单列表
     *
     * @param
     * @return array 数组格式的返回结果
     */
    public function getOrderList($condition = array(), $fields = '*', $order = '', $limit = '', $pagesize = '', $num = '')
    {
        $Order_list = $this->where($condition)->field($fields)->order($order)->limit($limit)->page($pagesize, $num)->select();
        if (empty($Order_list)) return array();
        return $Order_list;
    }


    /**
     * 取得单条订单信息
     * @param array $condition
     * @param string $order
     */
    public function getOrderInfo($condition, $fields = '*')
    {
        return $this->where($condition)->field($fields)->find();

    }


    /**
     * 取数量
     * @param unknown $condition
     */
    public function orderCount($condition = array())
    {
        return $this->where($condition)->count();
    }


    /**
     * 新增订单
     *
     * @param array $param 参数内容
     * @return bool 布尔类型的返回结果
     */
    public function addOrder($param, $goods_data, $goods_option)
    {
        $oid = $this->add($param);

        if ($oid) {

            //订单商品
            $this->execute("INSERT INTO " . C('DB_PREFIX') . "order_goods SET order_id = '" . $oid
                . "',goods_id='" . $goods_data['goods_id'] . "'"
                . ",name='" . $goods_data['name'] . "'"
                . ",model='" . $goods_data['model'] . "'"
                . ",quantity='" . (int)$param['quantity'] . "'"
                . ",price='" . (float)$goods_data['price'] . "'"
                . ",total='" . (float)$goods_data['price'] . "'"
            );

            //订单商品规格
            $order_goods_id = $this->getLastInsID();
            foreach ($goods_option as $option) {
                $this->execute("INSERT INTO " . C('DB_PREFIX') . "order_option SET order_id = '" . $oid
                    . "',order_goods_id='" . $order_goods_id . "'"
                    . ",goods_option_id='" . (int)$option['goods_option_id'] . "'"
                    . ",goods_option_value_id='" . (int)$option['goods_option_value_id'] . "'"
                    . ",name='" . $option['name'] . "'"
                    . ",value='" . $option['value'] . "'"
                    . ",type='" . $option['type'] . "'"
                );
            }

            //扣除库存
            $this->execute("UPDATE " . C('DB_PREFIX') . "goods SET quantity = (quantity - " . (int)1 . ") WHERE goods_id = '" . $goods_data['goods_id'] . "' AND subtract = '1'");

            return $oid;

        } else {
            return false;
        }


    }


    /**
     * 更新订单信息
     *
     * @param array $param 更新数据
     * @return bool 布尔类型的返回结果
     */
    public function editOrder($condition, $update)
    {
        return $this->where($condition)->save($update);
    }


    /**
     * 删除地址
     *
     * @param int $id 记录ID
     * @return bool 布尔类型的返回结果
     */
    public function delOrder($condition)
    {
        if (empty($condition)) {
            return false;
        }
        return $this->where($condition)->delete();
    }


}